package com.common.excel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;

/**
 *
 */
public class ExcelStyle implements CellWriteHandler {
    private Set<Integer> yellowRowIndexs;

    public ExcelStyle(Set<Integer> yellowRowIndexs) {
        this.yellowRowIndexs = yellowRowIndexs;
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
        Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell,
        Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    // @Override
    // public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData
    // cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    //
    // }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
        List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        if (!CollectionUtils.isEmpty(yellowRowIndexs)) {
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            // 字体
            Font cellFont = workbook.createFont();
            cellFont.setBold(true);
            cellStyle.setFont(cellFont);
            // 标黄,要一起设置
            // 设置前景填充样式
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            // 前景填充色
            cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

            if (yellowRowIndexs.contains(cell.getRowIndex())) {
                cell.setCellStyle(cellStyle);
            }
        }
    }
}
